package com.gopay.bis.manager.bank.ebank;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.bocom.ebank.pay.b2b.req.BocomB2bReq;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.DateUtils;
import com.gopay.remote.bank.BankManagerRemoteService;
import com.gopay.remote.bank.businesses.BankService;

public class BocomB2bManager implements BankManagerRemoteService{
	
	private static final String XMLHEADER_SIGN_B2B = "<?xml version=\"1.0\" encoding=\"gb2312\" ?>";
	
	@Resource(name = "bocomConfig")
	private Map<String, String> bocomConfig;

	@Resource(name = "bocomB2bBankServiceClient")
	private BankService bocomB2bBankServiceClient;

	@Resource(name = "bocomB2bBankServiceClientHna")
	private BankService bocomB2bBankServiceClientHna;

	
	/**
	 * B2B验签
	 * 
	 * @param BocomB2BReq
	 * @return BocomB2BReq
	 */
	public Object verify(BaseMessage message)  throws GopayException{
		boolean flag = false;
		BankLogUtils.printB2BManagerVerifySendLog(message);
		if(message.isHna()){
			flag = (Boolean) bocomB2bBankServiceClientHna.verify(message);
		}else{
			flag = (Boolean) bocomB2bBankServiceClient.verify(message);
		}
		BankLogUtils.printB2BManagerVerifyReturnLog(message, flag);
		return flag;
	}
	/**
     * B2B
     * 
     * @param message
     * @return
     */
    public BocomB2bReq query(Object obj)  throws GopayException{
    	BocomB2bReq message = (BocomB2bReq)obj;
    	BankLogUtils.printB2BManagerQuerySendLog(message, "");
    	BocomB2bReq b2bMessage;
		if(message.isHna()){
			b2bMessage = (BocomB2bReq) bocomB2bBankServiceClientHna.query(message);
		}else{
			b2bMessage = (BocomB2bReq) bocomB2bBankServiceClient.query(message);
		}
    	b2bMessage.setOutStlmCd(BankConstants.BANK_LOGO_BOCOM);
    	BankLogUtils.printB2BManagerQueryReturnLog(b2bMessage);
        return b2bMessage;
    }

	/**
	 * B2B签名
	 * 
	 * @param BocomB2BReq
	 * @return BocomB2BReq
	 */
	public Object sign(BaseMessage msg)  throws GopayException{	
		BocomB2bReq message = (BocomB2bReq)msg;
//		message.setMerchantNo(message.getMerID());
//		message.setOrderMode(bocomConfig.get("BOCOM.B2B.ORDERMODE"));
//		message.setValidatePeriod(bocomConfig.get("BOCOM.B2B.VALIDATEPERIOD"));
//		message.setBargainorEbankNo(bocomConfig.get("BOCOM.B2B.BARGAINOREBANKNO"));
//		message.setRecAccNo(bocomConfig.get("BOCOM.B2B.RECACCNO"));
//		message.setPayMode(bocomConfig.get("BOCOM.B2B.PAYMODE"));
//		message.setOrderCurrency(bocomConfig.get("BOCOM.B2B.ORDERCURRENCY"));
//		message.setRecBankType(bocomConfig.get("BOCOM.B2B.RECBANKTYPE"));
//		message.setBankUrl(message.getPayUrl());
//		message.setPurchaserFee(bocomConfig.get("BOCOM.B2B.PURCHASERFEE"));
//		message.setMerchantOrderNo(message.getOrderId());
//		message.setOrderName(message.getOrderName());
//		message.setPayAccNo(message.getCustomerID());
//		message.setOrderAmount(message.getAmount().toString());
//		message.setOrderComment("");
//		message.setBargainorName("");
//		message.setMdlComment("");
//		message.setBargainorComment("");
//		message.setPurchaserComment("");
//		message.setErpSerialNo("");
		
//		// 将对象格式化为XML
//		StringBuffer plain = new StringBuffer();
//		plain.append(XMLHEADER_SIGN_B2B);
//		XStream xstream = new XStream(new XppDriver(new XmlFriendlyNameCoder("_-", "_")));
//		xstream.autodetectAnnotations(true);
//		String plainMessage = xstream.toXML(message);
//		plain.append(plainMessage);
//		message.setPlainMessage(plain.toString());
//		BankLogUtils.printB2BManagerSignSendLog(message);
//		String signMessage; 
//		if(message.isHna()){
//			signMessage = (String) bocomB2bBankServiceClientHna.sign(message);
//		}else{
//			signMessage = (String) bocomB2bBankServiceClient.sign(message);
//		}
//		message.setSignMessage(signMessage);
//		BankLogUtils.printB2BManagerSignReturnLog(message);
//		return message;
//		

		Date date =DateUtils.strToDate(message.getOrderDate(), "yyyyMMddhhmmss");
		String orderDate = DateUtils.format(date, "yyyyMMdd");
		message.setOrderDate(orderDate);
		String orderTime = DateUtils.format(date, "hhmmss");
		message.setOrderTime(orderTime);
		//message.setMerID(message.getMerID());
		message.setOrderid(msg.getOrderId());
		message.setMerId(message.getMerID());
		message.setInterfaceVersion(bocomConfig.get("BOCOM.B2B.INTERFACEVERSION"));
		message.setTranType(bocomConfig.get("BOCOM.B2B.TRANTYPE"));
		message.setCurType(bocomConfig.get("BOCOM.B2B.CURTYPE"));
		message.setMerUrl(message.getPayRuturnUrl());
		//message.setGoodsUrl(message.getPayNotifyUrl());
		message.setNetType(bocomConfig.get("BOCOM.B2B.NETTYPE"));
		message.setNotifyType(bocomConfig.get("BOCOM.B2B.NOTIFYTYPE"));
		message.setBankUrl(message.getPayUrl());
		message.setRecvAcctNo(bocomConfig.get("BOCOM.B2B.RECACCNO"));
        if (StringUtils.isNotBlank(message.getPaymentAccInfo())) {
            message.setOrderContent(message.getPaymentAccInfo());
            message.setOrderMono(message.getPaymentAccInfo());
        }
        
		String plainMessage =buildPlain(message);
		message.setPlainMessage(plainMessage);
		BankLogUtils.printB2BManagerSignSendLog(message);
		String signMessage;
		if(message.isHna()){
		             signMessage = (String) bocomB2bBankServiceClientHna.sign(message);
	    }else{
		             signMessage = (String) bocomB2bBankServiceClient.sign(message);
	     }
		message.setSignMessage(signMessage);
		BankLogUtils.printB2BManagerSignReturnLog(message);
		return message;
	}
	@Override
	public Object dayend(BaseMessage base) throws GopayException, Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	 /**
     * 构建B2C明文
     * 
     * @param BOCOMMessageB2C
     * @return String
     */
    private String buildPlain(BocomB2bReq message) {
        StringBuffer sb = new StringBuffer();
        sb.append(message.getInterfaceVersion()).append("|");
        sb.append(message.getMerId()).append("|");
        sb.append(message.getOrderid()).append("|");
        sb.append(message.getOrderDate()).append("|");
        sb.append(message.getOrderTime()).append("|");
        sb.append(message.getTranType()).append("|");
        sb.append(message.getAmount()).append("|");
        sb.append(message.getCurType()).append("|");
        sb.append(message.getOrderContent()).append("|");
        sb.append(message.getOrderMono()).append("|");
        sb.append(message.getPhdFlag()).append("|");
        sb.append(message.getNotifyType()).append("|");
        sb.append(message.getMerUrl()).append("|");
        sb.append(message.getGoodsUrl()).append("|");
        sb.append(message.getJumpSeconds()).append("|");
        sb.append(message.getPayBatchNo()).append("|");
        sb.append(message.getProxyMerName()).append("|");
        sb.append(message.getProxyMerType()).append("|");
        sb.append(message.getProxyMerCredentials()).append("|");
        sb.append(message.getNetType()).append("|");
        sb.append(message.getOrderTimeout()).append("|");
        sb.append(message.getRecvAcctNo());
        return sb.toString();
    }
	@Override
	public Object refund(BaseMessage base) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
